import { PanDescConstant } from '../constants/constant_en';
import { Map, List } from 'immutable';
import { combineReducers } from 'redux';
import search_operator from './operator_reducer';
import search_command from './command_reducer';
import result_refine from './result_reducer';
import input from './input_reducer';



export const initState = {

    search_operator: Map({
        tab_name: PanDescConstant.pan_operator,
        content: Map({
            exact_match: Map({
                list: List(
                    [
                        ''
                    ]
                ),
                index: -1
            }),
            subtract: Map({
                list: List(
                    [
                        ''
                    ]
                ),
                index: -1
            }),
            synonyms: Map({
                list: List(
                    [
                        ''
                    ]
                ),
                index: -1
            }),
            may: Map({
                list: List(
                    [
                        ''
                    ]
                ),
                index: -1
            }),
            range: Map({
                from: '',
                to: ''
            })
        })
    }),
    search_command: Map({
        tab_name: PanDescConstant.pan_command,
        content: Map({
            site: '',
            inurl: Map({
                list: List(
                    [
                        ''
                    ]
                ),
                index: -1
            }),
            intitle: Map({
                list: List(
                    [
                        ''
                    ]
                ),
                index: -1
            }),
            inanchor: Map({
                list: List(
                    [
                        ''
                    ]
                ),
                index: -1
            }),
        })
    }),
    result_refine: Map({
        tab_name: PanDescConstant.pan_refine,
        content: Map({
            last_update: 0,
            file_type: 0
        })
    }),
    input: Map({
        input_text: ''
    })
}


/**
 * state 为一个Map，包含List，和index
 * edit某个inputGroup中的input，reduce当前的state
 * @param state
 * @param former_text
 * @param current_text
 * @param index
 * @param key
 */
export function reduceEditState(state, former_text, current_text, index) {
    let list = state.get('list')
    //0表示没有空项，在末尾加一个空项，1表示有一个空项，2表示有两个空项，初始有一个空项
    let empty = 1;
    if (!former_text && current_text) {
        empty = 0;
    } else if (former_text && !current_text) {
        empty = 2;
    }
    if (empty == 0) {
        list = list.update(index, ()=>current_text).push('');
    } else if (empty == 2) {
        list = list.delete(index);
    } else {
        list = list.update(index, ()=>current_text);
    }
    state = state.set('list', list).set('index', index);
    return state;
}

const search_reducer = combineReducers({
    search_operator,
    search_command,
    result_refine,
    input
});
export default search_reducer;